package com.example.minefieldkeepercloud.adapter;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.bean.PoolBean;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.dialog.AddDialog;
import com.king.base.adapter.ViewHolderRecyclerAdapter;
import com.king.base.adapter.holder.ViewHolder;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SetAdapter extends ViewHolderRecyclerAdapter<PoolBean> {
    private Activity context;
    private Map<String,String> map=new HashMap<>();
    private Map<EditText,String> map_edit1=new LinkedHashMap <>();//矿池
    private Map<EditText,String> map_edit2=new LinkedHashMap<>();//矿工
    public SetAdapter(Activity context, List<PoolBean> listData) {
        super(context, listData);
        this.context = context;

    }


    @Override
    public View buildConvertView(LayoutInflater layoutInflater, int viewType) {
        return layoutInflater.inflate(R.layout.adapter_poolset,null);
    }
    @Override
    public void bindViewDatas(ViewHolder holder, final PoolBean bean, final int position) {

        holder.setText(R.id.tv_1,bean.getKC());
        holder.setText(R.id.tv_2,bean.getKGM());

       final EditText editText1=holder.getView(R.id.tv_1);
       final EditText editText2=holder.getView(R.id.tv_2);
        map_edit1.put(editText1,bean.getID());
        map_edit2.put(editText2,bean.getID());

        editText1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                getListData().get(position).setKC(editText1.getText().toString());
            }
        });
        editText2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                getListData().get(position).setKGM(editText2.getText().toString());
            }
        });
        ImageView imageView=holder.getView(R.id.add);
//        if(getListData().size()-1==position)//判断是不是最后一个
//        {
//            imageView.setImageResource(R.mipmap.tianjia);
//            imageView.setOnClickListener(new View.OnClickListener() {//添加
//                @Override
//                public void onClick(View v) {
//                    add();
//                }
//            });
//        }
//        else
//        {
//            imageView.setImageResource(R.mipmap.delete);
//            imageView.setOnClickListener(new View.OnClickListener() {//删除
//                @Override
//                public void onClick(View v) {
//                    delete(position,editText1,editText2);
//                }
//            });
//        }
        imageView.setImageResource(R.mipmap.delete);
        imageView.setOnClickListener(new View.OnClickListener() {//删除
            @Override
            public void onClick(View v) {
                delete(position,editText1,editText2);
            }
        });
//        map.put(bean.getKGM()+"|"+bean.getKC(),bean.getID());


    }

    public void add()
    {
        AddDialog addDialog=new AddDialog(context, R.style.dialog, new AddDialog.OnAddClickListener() {
            @Override
            public void add(String strKc, String strKg) {
                if(isLive(strKc,strKg))
                {
                    PoolBean poolBean=new PoolBean();
                    poolBean.setKC(strKc);
                    poolBean.setKGM(strKg);
                    poolBean.setID("0");
                    getListData().add(poolBean);
                    notifyDataSetChanged();
                }
                else
                {
                    ToastUtil.showToast(context,"不能添加一样的矿池及矿工名");
                }


            }
        });
        addDialog.show();
    }

    public void add(final ImageView iv)
    {
        AddDialog addDialog=new AddDialog(context, R.style.dialog, new AddDialog.OnAddClickListener() {
            @Override
            public void add(String strKc, String strKg) {
                    iv.setVisibility(View.GONE);
                    PoolBean poolBean=new PoolBean();
                    poolBean.setKC(strKc);
                    poolBean.setKGM(strKg);
                    poolBean.setID("0");
                    getListData().add(poolBean);
                    notifyDataSetChanged();

            }
        });
        addDialog.show();
    }
    private void delete(int p,EditText editText1,EditText editText2)
    {
        getListData().remove(p);
        map_edit1.remove(editText1);
        map_edit2.remove(editText2);
        notifyDataSetChanged();
    }

    public JSONObject getMap()
    {
        if(getListData().size()>0)
        {
            map.clear();
            List<String> list1=new ArrayList();
            List<String> list2=new ArrayList();
            List<String> list3=new ArrayList();
            for(Map.Entry<EditText, String> entry : map_edit1.entrySet()){
                list1.add(entry.getKey().getText().toString());
                list3.add(entry.getValue());
            }
            for(Map.Entry<EditText, String> entry : map_edit2.entrySet()){
                list2.add(entry.getKey().getText().toString());
            }

            for (int i=0;i<list1.size();i++)
            {
                map.put(list2.get(i)+"|"+list1.get(i),list3.get(i));
            }

            JSONObject jsonObj=new JSONObject(map);
            return jsonObj;
        }

        else
            return null;
    }

    private boolean isLive(String strKc, String strKg)
    {
        String str=strKg+"|"+strKc;
        Log.d("AAA",str);
        for (int i=0;i< getListData().size();i++)
        {
            Log.d("BBBB",getListData().get(i).getKGM().toLowerCase()+"|"+getListData().get(i).getKC().toLowerCase());
            if(str.toLowerCase().equals(getListData().get(i).getKGM().toLowerCase()+"|"+getListData().get(i).getKC().toLowerCase()))
            {
                Log.d("CCCC",getListData().get(i).getKGM().toLowerCase()+"|"+getListData().get(i).getKC().toLowerCase());
                return false;
            }
        }
        return true;
    }
}
